package abc.com.abc.activity;


import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ShareActionProvider;
import android.widget.TextView;
import android.widget.Toast;

import abc.com.abc.R;
import abc.com.abc.utils.AnalysisiUtils;
import abc.com.abc.utils.MD5Utils;

public class FindPswActivity extends AppCompatActivity {
    private EditText et_validate_idcard,et_user_name;
    private Button btn_validate;
    private TextView tv_main_title,tv_back;
    private  String from;
    private TextView tv_reset_psw,tv_username;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_psw);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//设置竖屏
        //获取从登陆界面传过来的数据
        //Intent intent = getIntent();
        from = getIntent().getStringExtra("from");
        if(from == null)
        {
            from = "FindPwd";
        }
        init();

    }

    private void init() {
        et_validate_idcard = findViewById(R.id.et_validate_idcard);
        et_user_name = findViewById(R.id.et_user_name);
        btn_validate = findViewById(R.id.bt_validate);
        tv_main_title = findViewById(R.id.tv_main_title);
        tv_back = findViewById(R.id.tv_back);
        tv_reset_psw=findViewById(R.id.tv_reset_psw);
        tv_username = findViewById(R.id.tv_user_name);
        System.out.println(from);
        if ("security".equals(from)){

            tv_main_title.setText("设置密保");
        }else {
            tv_main_title.setText("找回密码");
            tv_username.setVisibility(View.VISIBLE);
            et_user_name.setVisibility(View.VISIBLE);
        }
        tv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FindPswActivity.this.finish();
            }
        });
        btn_validate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String validateName = et_validate_idcard.getText().toString().trim();
                if (from.equals("security")){//设置密保
                    if (TextUtils.isEmpty(validateName)){
                        Toast.makeText(FindPswActivity.this,"请输入身份证后6位",Toast.LENGTH_SHORT).show();
                        return;
                    }else {
                        Toast.makeText(FindPswActivity.this,"密保设置成功",Toast.LENGTH_LONG).show();
                        saveSecurit(validateName);
                        FindPswActivity.this.finish();
                    }
                }else {
                    //找回密码
                    String userName = et_user_name.getText().toString().trim();
                    String sp_security = readSecurity(userName).toString().trim();
                    if (TextUtils.isEmpty(userName)){
                        Toast.makeText(FindPswActivity.this,"请输入用户名",Toast.LENGTH_LONG).show();
                        return;
                    }else  if (!isExisUserName(userName)){
                        Toast.makeText(FindPswActivity.this,"用户名不存在",Toast.LENGTH_LONG).show();
                        return;
                    }else  if (TextUtils.isEmpty(validateName)){
                        Toast.makeText(FindPswActivity.this,"请输入验证的身份证后6位",Toast.LENGTH_LONG).show();

                        return;
                    }if (!validateName.equals(sp_security)){
                        Toast.makeText(FindPswActivity.this,"输入密保不正确",Toast.LENGTH_LONG).show();

                        return;
                    }else {
                        //密保真确，要重设置密码
                        tv_reset_psw.setVisibility(View.VISIBLE);
                        tv_reset_psw.setText("初始密码:"+"123456");
                        savePsw(userName);
                    }

                }
            }
        });
    }
//保存初始化密码
    private void savePsw(String userName) {
        String md5Psw = MD5Utils.md5("123456");
        SharedPreferences sp = getSharedPreferences("loginInfo",MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(userName,md5Psw);
        editor.commit();
    }
    //密保保存到Sharedpreferences 中
    private void saveSecurit(String validateName) {
        SharedPreferences sp = getSharedPreferences("loginInfo",MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(AnalysisiUtils.readLoginUserName(this)+"_security",validateName);
        editor.commit();

    }

    //根据用户名的输入来判断有无此用户名
    private boolean isExisUserName(String userName) {
        boolean hasUserName = false;
        SharedPreferences sp = getSharedPreferences("loginInfo",MODE_PRIVATE);
        String spPsw = sp.getString(userName,"");
        if (!TextUtils.isEmpty(spPsw)){
            hasUserName = true;
        }
            return hasUserName;
        }


//从sharedpreferences中读取密保
    private String readSecurity(String userName) {
        SharedPreferences sp = getSharedPreferences("loginInfo",MODE_PRIVATE);
        String security = sp.getString(userName+"_security","");
        System.out.println(">>>"+security);
        return security;
    }

}
